﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data.Entity;

using MvcCms.Data;

namespace MvcCms.Data.Service.Code
{
    /// <summary>
    /// Provides Non AR/Public... Specific Payment Data To WPO*.Service projects - Business Logic Should Be Placed Here Unless Data Specific such as limiting results returned.
    /// Do ToList here.
    /// </summary>
    public class StoreRepository : IStoreRepository
    {
        #region Constructors

        private readonly MvcCmsEntities _entities;
        private readonly IEntitiesProvider _entitiesProvider;

        public StoreRepository(IEntitiesProvider entitiesProvider)
        {
            _entities = entitiesProvider.GetEntityContext();
            _entitiesProvider = entitiesProvider;
        }

        #endregion

        public void CreatePriceOption(StorePricingOption priceOptionToCreate)
        {
            _entities.StorePricingOptions.AddObject(priceOptionToCreate);
            _entities.SaveChanges();
        }

        public void CreateProduct(StoreProduct productToCreate)
        {
            _entities.StoreProducts.AddObject(productToCreate);
            _entities.SaveChanges();
        }

        public void CreateOrder(StoreOrder orderToCreate)
        {
            _entities.StoreOrders.AddObject(orderToCreate);
            _entities.SaveChanges();
        }

        public IEnumerable<StoreOrder> ListOrders()
        {
            return (from c in _entities.StoreOrders
                    select c);
        }

        public IEnumerable<StoreShippingMethodRate> ListShippingMethodRates()
        {
            return (from c in _entities.StoreShippingMethodRates
                    select c);
        }

        public IEnumerable<StoreCart> ListStoreCartItems()
        {
            return (from c in _entities.StoreCarts
                    select c);
        }

        public IEnumerable<StoreProduct> ListProducts()
        {
            return (from c in _entities.StoreProducts
                    select c);
        }

        public IEnumerable<StorePricingOption> ListPriceOptions()
        {
            return (from c in _entities.StorePricingOptions
                    select c);
        }

        public IEnumerable<StoreCategory> ListCategories()
        {
            return (from c in _entities.StoreCategories
                    select c);
        }

        public StoreProduct EditProduct(StoreProduct productToEdit)
        {
            _entities.StoreProducts.ApplyCurrentValues(productToEdit);
            _entities.SaveChanges();
            return productToEdit;
        }
        public StorePricingOption EditPriceOption(StorePricingOption pricingOptionToEdit)
        {
            _entities.StorePricingOptions.ApplyCurrentValues(pricingOptionToEdit);
            _entities.SaveChanges();
            return pricingOptionToEdit;
        }
        public StoreCart EditCart(StoreCart cartToEdit)
        {
            _entities.StoreCarts.ApplyCurrentValues(cartToEdit);
            _entities.SaveChanges();
            return cartToEdit;
        }
        public void DeleteCart(StoreCart cartToDelete)
        {
            _entities.DeleteObject(cartToDelete);
            _entities.SaveChanges();
        }
        public void CreateCart(StoreCart cartToCreate)
        {
            _entities.StoreCarts.AddObject(cartToCreate);
            _entities.SaveChanges();
        }
    }
}
